package com.example.springdemo.Controller;

import com.auth0.jwt.JWT;
import com.example.springdemo.entity.VO.LoginUser;
import com.example.springdemo.utils.JWTUtils;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/test")
    public String test() {
        //读取redis中的数据
        String s = (String) redisTemplate.opsForValue().get("data");  //key的值是name
        redisTemplate.opsForValue().set("age", "21", 20, TimeUnit.SECONDS);
        return s;
    }

    @PostMapping("/login1")
    public ResponseResult<String> login(@RequestBody LoginUser loginUser) {
        //先判断验证码是否正确
        //1.如何从redis取出保存的验证码
        String storeCode = (String) redisTemplate.opsForValue().get("captcha_codes:"+loginUser.getUuid());
        //2.用户输入的验证码和storeCode比对
                if(loginUser.getCode().equals(storeCode)){
                    String id = loginUser.getUsername();
                    String pwd = loginUser.getPassword();
                    Map<String,String> map = new HashMap<>();
                    map.put("username",id);
                    map.put("password",pwd);

                    if (id.equals("admin") && pwd.equals("admin111")) {
                        String token = JWTUtils.getToken(map);
                        return ResponseResult.createBySuccess(200, token, "登录成功");
                    }
                    return ResponseResult.createBySuccess(500, "", "账号或者密码错误");

                }else{
                    return ResponseResult.createBySuccess(500,"","验证码错误");
                }
    }

    @PassToken
    @GetMapping("/getStudents")
    public ResponseResult<String> getStudents() {
        return ResponseResult.createBySuccess(200, "", "查询成功");
    }


    //token无效
//   @GetMapping("/getInfo1")
//    public ResponseResult<String> getInfo(HttpServletRequest request){
//        Map<String,Object> map = new HashMap<>();
//        map.put("name","张三");
//        map.put("avatar","1.jpg");
       //如何根据token获取用户ID
       //从token获得id
//       String token = request.getHeader("Authorization");
//       System.out.println(token+"  ");
//       String id= JWT.decode(token.substring(token.lastIndexOf(" ")+1)).getClaim("username").asString();
//
//        return ResponseResult.createBySuccess(200,id,"查询成功");
//    }


@PassToken
@GetMapping("/getBanners")
public ResponseResult< List<String>> getBanners(){
    List<String> list = new ArrayList<>();  //轮播图资源
    list.add("/images/3.jpg"); list.add("/images/4.jpg"); list.add("/images/5.jpg"); list.add("/images/6.jpg");
    return ResponseResult.createBySuccess(200,list,"显示轮播图成功");
}


}








